using System;
using System.Collections.Generic;
using System.Text;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace identifyinglocation
{
    class identifyinglocation
    {
        static void Main(string[] args)
        {
            // create a sql parser instance supports ms sql server sql dialect
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);

            StringBuilder sb = new StringBuilder(1024);
            sb.Append("select empname,sal from emp\n");
            sb.Append("where sal > 2000 and deptno = 10 order by sal");

            Console.WriteLine("Input sql\n{0}", sb.ToString());

            sqlparser.SqlText.Text = sb.ToString();
            int iRet = sqlparser.Parse();
            if (iRet == 0)
            {
                TSelectSqlStatement stmt = (TSelectSqlStatement)sqlparser.SqlStatements[0];
                TLzCustomExpression whereExpr = stmt.WhereClause;
                TSourceToken starttoken = whereExpr.StartToken;
                TSourceToken endtoken = whereExpr.EndToken;

                Console.WriteLine("Start pos of where clause: {0},{1}",starttoken.XPosition,starttoken.YPosition-starttoken.AsText.Length);
                Console.WriteLine("End pos of where clause: {0},{1}", endtoken.XPosition, endtoken.YPosition);
                
                TLzOrderByList orderbyClause = stmt.SortClause;
                TSourceToken starttoken2 = orderbyClause.StartToken;
                TSourceToken endtoken2 = orderbyClause.EndToken;
                Console.WriteLine("Start pos of order by: {0},{1}", starttoken2.XPosition, starttoken2.YPosition - starttoken2.AsText.Length);
                Console.WriteLine("End pos of order by: {0},{1}", endtoken2.XPosition, endtoken2.YPosition);

               
            }
            else
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);
            }

        }
    }
}
